﻿//-----------------------------------------------------------------------
// <copyright file="User.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//----------------------------------------------------------------------

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Knihotoc_WP8.Data.Storage;
using Knihotoc_WP8.Localization;
using System.Collections.Generic;

namespace Knihotoc_WP8.Data
{
    public class User : IIsolatedStorage<User>
    {
        #region Constructors
        public User()
        {
            BorrowedBooks = new List<Book>();
            FoundBooks = new List<Book>();
            MyBooks = new List<Book>();
        }

        public User(int id, string userName, string password) 
            : this ()
        {
            this.ID = id;

            if (userName != null && userName.Length > 0)
            {
                this.UserName = userName;
            }

            if (password != null && password.Length > 0)
            {
                this.Password = password;   // TODO zasifrovat heslo
            }

            this.SaveToIsolatedStorage();
        }
        #endregion

        #region Public properties
        /// <summary>
        /// Gets or sets the user's ID.
        /// </summary>
        public int ID { get; set; }
        
        /// <summary>
        /// Gets or sets the user's first name.
        /// </summary>
        public string FirstName { get; set; }
        
        /// <summary>
        /// Gets or sets the user's surname.
        /// </summary>
        public string SurName { get; set; }
        
        /// <summary>
        /// Gets or sets the user's password.
        /// </summary>
        public string Password { get; set; }
        
        /// <summary>
        /// Gets or sets the link to user's profile picture.
        /// </summary>
        public string ImageLink { get; set; }
        
        /// <summary>
        /// Gets or sets the username.
        /// </summary>
        public string UserName { get; set; }
        
        /// <summary>
        /// Gets or sets the user's email.
        /// </summary>
        public string Email { get; set; }
        /// <summary>
        /// Gets or sets total number of borrowed books
        /// </summary>
        public int TotalBorrowed { get; set; }

        /// <summary>
        /// Gets or sets total number of found books
        /// </summary>
        public int TotalFound { get; set; }

        /// <summary>
        /// Gets or sets list of actually borrowed books
        /// </summary>
        public List<Book> BorrowedBooks { get; set; }
        
        /// <summary>
        /// Gets or sets list of books user owns
        /// </summary>
        public List<Book> MyBooks { get; set; }

        /// <summary>
        /// List of books that user found 
        /// </summary>
        public List<Book> FoundBooks { get; set; }
        #endregion

        public bool SaveToIsolatedStorage()
        {
            bool everythingAdded = true;

            if (this.UserName != null)
            {
                if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("User_" + this.ID.ToString() + "_UserName", this.UserName))
                {
                    everythingAdded = false;
                }
            }
            else
            {
                return false;
            }

            if (this.Password != null)
            {
                if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("User_" + this.ID.ToString() + "_Password", this.Password))
                {
                    everythingAdded = false;
                }
            }
            else
            {
                everythingAdded = false;
            }

            if (everythingAdded)
            {
                IsolatedStorageHandler.Instance.Save();
                return true;
            }
            else
            {
                IsolatedStorageHandler.Instance.RemoveValue("User_" + this.ID.ToString() + "_UserName");
                IsolatedStorageHandler.Instance.RemoveValue("User_" + this.ID.ToString() + "_Password");

                return false;
            }
        }

        public void LoadFromIsolatedStorage(string id)
        {
            this.UserName = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "User_" + id + "_UserName", LocalizedStrings.Get("UserNameNotFound"));

            this.Password = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "User_" + id + "_Password", LocalizedStrings.Get("PasswordNotFound"));
        }
    }
}
